﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;


namespace SystemX.WebControls//Note:The namespace is different from SiteMaster!
{
    /// <summary>
    /// wrapper control for css
    /// </summary>
    public class SiteCSS : WebControl
    {
        private string cssRelativeToRoot = string.Empty;
		/// <summary>
		/// relative path of the css.Or the absolute path starts with http://
		/// </summary>
        public string CSSRelativeToRoot
        {
            get { return cssRelativeToRoot; }
            set { cssRelativeToRoot = value; }
        }


        /// <summary>
        /// 是否末尾加换行符
        /// </summary>
        public bool Linebreak { get; set; }


        private string defaultMediaType="screen";
        /// <summary>
        /// 样式使用类型
        /// </summary>
        public string MediaType
        {
            get{return defaultMediaType;}
            set{defaultMediaType=value;}
        }

        public string Script
        {
            get {
                var path = cssRelativeToRoot.ToLower().StartsWith("http://") ? cssRelativeToRoot : string.Format("{0}{1}", SystemX.Web.Utils.AbsoluteWebRoot, cssRelativeToRoot);
				return String.Format("{0}<link href='{1}' rel='stylesheet' type='text/css' media='{2}' />", (Linebreak ? System.Environment.NewLine : ""),path, MediaType); 
			
			}

        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.Site != null && this.Site.DesignMode)
            {
                // TODO: show a bmp or some other design time thing?
                //writer.Write("[" + this.ID + "]");
            }
            else
            {
                writer.Write(this.Script);
            }
        }

    }
}
